layui.define(['element','carousel','dataGrid','layedit','form'], function(exports){
	var element = layui.element;
	var carousel = layui.carousel;
	var $ = layui.$;
	var dataGrid = layui.dataGrid;
    var layedit = layui.layedit;
    var admin = layui.admin;
    var form = layui.form;

	
	
	//图片轮播
    carousel.render({
      elem: '#test10'
      ,width: '100%'
      ,height: '440px'
      ,interval: 5000
    });
	   
    window.Detail = function(id){
        location.href= "/default/detail3/"+id+"/";
    }
    
    // window.Detail2 = function(id){
    //     location.href= "/default/detail2/"+id+"/";
    // }
    
    var ins = dataGrid.render({
        elem: '#demoCardList1',
        templet: '#demoCardItem1',
        url: '/default/present/page',
        loadMore: {limit: 12}
    });
    
    var ins = dataGrid.render({
        elem: '#demoCardList2',
        templet: '#demoCardItem2',
        where:{state:1},
        url: '/default/release/page',
        loadMore: {limit: 12}
    });

    var editIndex1 = layedit.build('eContentEdt1'); // 建立编辑器

    //Article评论部分
    window.SaveArticleRemark = function (id){
        var content = layedit.getContent(editIndex1);
        console.log(id);
        console.log(content);
        $.ajax({
            type:"post",
            url:"/default/SaveArticleRemark",
            data:{"id":id,"content":content},
            success:function (res){
                if(0 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    window.setTimeout(window.location.reload(),5000);
                }else if(1 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(2 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }
            }
        });
    }

    window.ReplyArticleRemark = function (id){
        var remarkId = parseInt(id);
        admin.open({
            type: 1,
            area: ['1000px','600px'],
            title: '回复评论',
            content: $('#remarkEdit').html(),
            success: function (layero, dIndex) {
                if(id){
                    $('#eContentEdt').html(id.content);
                }

                var editIndex = layedit.build('remarkContentEdt'); // 建立编辑器

                // 回显表单数据
                form.val('courseEditForm1', id);

                // 表单提交事件
                form.on('submit(replyRemarkDemo)', function (data) {
                    data.field.content = layedit.getContent(editIndex);
                    console.log(data.field.content);
                    console.log(remarkId);
                    var loadIndex = layer.load(2);
                    $.post('/default/replyArticleRemark', {parentId:remarkId,content:data.field.content}, function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.close(dIndex);
                            layer.msg(res.msg, {icon: 1});
                            window.setTimeout(window.location.reload(),5000);
                            //window.location.reload();
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    });
                    return false;
                });
            }
        });
    }

    window.AddArticleRemarkLikes = function (id){
        console.log(id);
        $.ajax({
            type:"post",
            url:"/default/addArticleRemarkLikes",
            data:{"id":id,"type":1},
            success:function (res){
                if(0 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    // $("#likes").css("color","red");
                    window.setTimeout(window.location.reload(),5000);
                }else if(1 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(2 === res.code){
                    layer.msg(res.msg, {icon: 1});
                }else if(3 === res.code){
                    layer.msg(res.msg, {icon: 1});
                    // $("#likes").css("color","#99a2aa");
                    window.setTimeout(window.location.reload(),5000);
                }
            }
        });
    }


    exports('default3', null);
});